﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using BrnMall.Manager.Domain.Messages;
using BrnMall.Manager.Manager.Messages;
using BrnMall.Manager.Manager.Store;
using BrnMall.Services;
using BrnMall.Web.Framework;
using BrnMall.Web.StoreAdmin.Models;
using Newtonsoft.Json;

namespace BrnMall.Web.StoreAdmin.Controllers
{
    public partial class MessageController : BaseStoreAdminController
    {
        /// <summary>
        /// 留言列表
        /// </summary>
        /// <returns></returns>
        public ActionResult MessageRow(int pageNumber = 1, int pageSize = 15)
        {
            var listModel = MessagesManager.GetListPage(WorkContext.StoreId, pageNumber, pageSize);
            var model = new Models.MessageListModel();

            model.PageModel = new PageModel(pageSize, pageNumber, (int)listModel.TotalItems);
            model.List = listModel.Items;

            MallUtils.SetAdminRefererCookie($"{Url.Action("MessageRow")}?pageNumber={pageNumber}&pageSize={pageSize}");
            return View(model);
        }

        /// <summary>
        /// 审核页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Check(int Msgid)
        {
            var widget = WidgetManager.GetWidget(WorkContext.StoreId);
            var uploadTips = string.Empty;
            var widgetModel = new WidgetModel
            {
                Code = widget.Code,
                Name = widget.Name,
                StoreId = widget.StoreId,
                WId = widget.WId
            };
            var config = WidgetManager.GetConfigList(widget.WId).Select(t => new WidgetConfigModel
            {
                CId = t.CId,
                IsShow = t.IsShow,
                LinkType = t.LinkType,
                LinkValue = JsonConvert.DeserializeObject<LinkValue>(t.LinkValue),
                ShowName = t.ShowName,
                ShowImg = t.ShowImg,
                DisplayOrder = t.DisplayOrder,
                WId = t.WId,
                DisplayCount = t.DisplayCount,
                DisplayType = t.DisplayType
            }).ToList();

            var model = new WidgetConfigListModel
            {
                Config = config,
                Widget = widgetModel,
                StoreClassList = Stores.GetStoreClassList(WorkContext.StoreId),
                NewsTypeList = News.GetStoreNewsTypeList(WorkContext.StoreId),
                PageInfos = PageInfoConfig.Pages,
                UploadImageTips = uploadTips
            };
            var Msgmodel = MessagesManager.GetMsg(Msgid);
            ViewData["model"] = Msgmodel;
            return View(model);
        }

        public ActionResult DeleteMessage(int MsgId)
        {
            MessagesManager.DeleteMsg(MsgId);
            return PromptView("删除成功");
        }

        /// <summary>
        /// 商家审核
        /// </summary>
        /// <param name="Msgid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(int Msgid, string LinkType, string Pid, string ProductName, string StoreClassId, string StoreClassName, string NewsId, string NewsName, string NewsTypeId, string NewsTypeName, string PageInfoId, string PageName, string H5PageId, string H5PageName, string PhoneNumberId, string PhoneNumberName,string Comment, int Ischeck)
        {
            var linkValue = new LinkValue() { Id = string.Empty, LinkType = string.Empty };
            switch (LinkType)
            {
                case "product":
                    if (string.IsNullOrEmpty(Pid))
                        return PromptView("链接到商品时，没有指定商品");
                    linkValue.Id = Pid;
                    linkValue.Name = ProductName;
                    linkValue.LinkType = "product";
                    break;
                case "store_class":
                    if (string.IsNullOrEmpty(StoreClassId))
                        return PromptView("链接到店铺分类时，没有指定店铺分类");
                    linkValue.LinkType = "store_class";
                    linkValue.Id = StoreClassId;
                    linkValue.Name = StoreClassName;
                    break;
                case "news":
                    if (string.IsNullOrEmpty(NewsId))
                        return PromptView("链接到内容时，没有指定内容");
                    linkValue.LinkType = "news";
                    linkValue.Id = NewsId;
                    linkValue.Name = NewsName;
                    break;
                case "news_type":
                    if (string.IsNullOrEmpty(NewsTypeId))
                        return PromptView("链接到内容分类时，没有指定内容分类");
                    linkValue.LinkType = "news_type";
                    linkValue.Id = NewsTypeId;
                    linkValue.Name = NewsTypeName;
                    break;
                case "page":
                    if (string.IsNullOrEmpty(PageInfoId))
                        return PromptView("链接到页面时，没有指定页面");
                    var pageInfo = PageInfoConfig.Get(PageInfoId);
                    linkValue.Id = pageInfo.Id;
                    linkValue.Name = PageName;
                    linkValue.LinkType = "page";
                    break;

                case "h5page":
                    if (string.IsNullOrEmpty(H5PageId))
                        return PromptView("链接到H5网页时，没有指定网页路径");
                    linkValue.Id = H5PageId;
                    linkValue.Name = H5PageName;
                    linkValue.LinkType = "h5page";
                    break;
                case "phonecall":
                    if (string.IsNullOrEmpty(PhoneNumberId))
                        return PromptView("链接到一键拨号时，没有指定号码");
                    linkValue.Id = PhoneNumberId;
                    linkValue.Name = PhoneNumberName;
                    linkValue.LinkType = "phonecall";
                    break;
                default:
                    linkValue.Id = string.Empty;
                    linkValue.Name = string.Empty;
                    linkValue.LinkType = string.Empty;
                    break;
            }

            MessagesModel model = MessagesManager.GetMsg(Msgid);

            model.IsCheck = Ischeck;
            model.Comment = Comment;
            if (!string.IsNullOrEmpty(LinkType))
            {
                model.LinkType = LinkType;
                model.LinkValue = JsonConvert.SerializeObject(linkValue);
            }

            MessagesManager.Check(model);
            return PromptView("ok");            
        }

        /// <summary>
        /// 商家评论
        /// </summary>
        /// <param name="Msgid"></param>
        /// <param name="Comment"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddMegComment(int Msgid, string Comment)
        {
            var model = MessagesManager.GetMsg(Msgid);
            var admincomment = model.Comment;
            if (admincomment == null || admincomment == "")
            {
                model.Comment = Comment;
                bool re = MessagesManager.UpdateMsg(model);
                if (re)
                    return PromptView("评论成功");
                else
                    return PromptView("评论失败，请稍后再试");
            }
            else
                return PromptView("已评论过了！");
        }

        //创意设计
        public ActionResult CreateDesign()
        {
            var model = CreateDesignManager.GetDesign(WorkContext.StoreId);
            if (model == null)
                model = new CreateDesigns();
            ViewData["model"] = model;
            return View();
        }

        /// <summary>
        /// 添加创意标题页面
        /// </summary>
        /// <returns></returns>
        public ActionResult DesignEdit(string DesignName)
        {
            var isTrue = false;
            var model = CreateDesignManager.GetDesign(WorkContext.StoreId);
            if (model == null)
            {
                model = new CreateDesigns() { StoreId = WorkContext.StoreId, DesignName = DesignName };
                isTrue = CreateDesignManager.AddDesign(model);
            }
            else
                isTrue = CreateDesignManager.Edit(WorkContext.StoreId, DesignName);

            if (isTrue)
                return Content("ok");
            return Content("no");
        }

        /// <summary>
        /// 返回创意设计的数据接口
        /// </summary>
        /// <returns></returns>
        public string GetDesign()
        {
            CreateDesigns mm = CreateDesignManager.GetDesign(WorkContext.StoreId);
            var model = JsonConvert.SerializeObject(mm);
            return model;
        }
    }
}